package service

import (
	"github.com/mojocn/base64Captcha"
	"image/color"
	"strings"
	"time"
)

// VerifyCaptcha 校验验证码
func VerifyCaptcha(id, code string) bool {
	value := store.Get(id, false)
	if strings.ToLower(code) != strings.ToLower(value) {
		return false
	}
	store.Get(id, true)
	return true
}

var store = base64Captcha.NewMemoryStore(10240, 3*time.Minute)

// CreateCaptchaByDigit 创建图形化数字验证码配置
func CreateCaptchaByDigit() (id, b64 string, err error) {
	var driver base64Captcha.Driver
	driver = &base64Captcha.DriverDigit{
		Height:   60,
		Width:    160,
		Length:   5,
		MaxSkew:  0.45,
		DotCount: 80,
	}
	capt := base64Captcha.NewCaptcha(driver, store)
	id, b64, err = capt.Generate()
	return
}

// CreateCaptchaByString 创建图形化字符串验证码配置
func CreateCaptchaByString() (id, b64 string, err error) {
	var driver base64Captcha.Driver
	driver = &base64Captcha.DriverString{
		Height:          60,
		Width:           160,
		Length:          5,
		NoiseCount:      0,
		ShowLineOptions: base64Captcha.OptionShowHollowLine | base64Captcha.OptionShowSlimeLine,
		Source:          "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
		BgColor: &color.RGBA{
			R: 40,
			G: 30,
			B: 89,
			A: 29,
		},
		Fonts: nil,
	}
	capt := base64Captcha.NewCaptcha(driver, store)
	id, b64, err = capt.Generate()
	return
}
